package com.hoshiicloud.user.service.impl.company;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.user.entity.company.OrganizationType;
import com.hoshiicloud.user.feign.IdGeneratorService;
import com.hoshiicloud.user.mapper.company.IOrganizationTypeMapper;
import com.hoshiicloud.user.service.company.IOrganizationTypeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 机构类型 服务实现类
 * </p>
 *
 * @author WGJ
 * @since 2019-04-22
 */
@Service
public class OrganizationTypeServiceImpl extends ServiceImpl<IOrganizationTypeMapper, OrganizationType> implements IOrganizationTypeService {
    @Autowired
    private IdGeneratorService idGeneratorService;

    @Autowired
    private IOrganizationTypeMapper organizationTypeMapper;

    /**
     * 根据id查询机构类型信息
     * @param id 机构类型ID
     * @return OrganizationType
     */
    @Cacheable(cacheNames = "organizationtype", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "租户管理-机构类型管理-根据id查询机构类型信息")
    @Override
    public OrganizationType getById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据id查询机构类型信息
     * @param ids 机构类型ID组
     * @return List<OrganizationType>
     */
    @Cacheable(cacheNames = "organizationtype", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#ids")
    @Logable(authName = "租户管理-机构类型管理-根据id组查询机构类型信息")
    @Override
    public List<OrganizationType> getByIds(List<Long> ids) {
        return baseMapper.selectBatchIds(ids);
    }

    /**
     * 新增机构类型信息
     * @param entity 新增参数
     * @return Long 主键ID
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "organizationtype", allEntries = true)
    @Logable(authName = "租户管理-机构类型管理-新增机构类型信息")
    @Override
    public Long insert(OrganizationType entity) throws ServiceException {
        entity.setCreatedTime(LocalDateTime.now());
        entity.setModifiedTime(LocalDateTime.now());
        entity.setCreatedBy("admin");
        entity.setModifiedBy("admin");
        entity.setEnabled(0);
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增机构类型失败"));
        }
        return entity.getId();
    }

    /**
     * 修改机构类型信息
     * @param entity 修改参数
     * @return Long 主键ID
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "organizationtype", allEntries = true)
    @Logable(authName = "租户管理-机构类型管理-修改机构类型信息")
    @Override
    public Long update(OrganizationType entity) throws ServiceException {
        entity.setModifiedTime(LocalDateTime.now());
        entity.setModifiedBy("admin1");
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改机构类型失败"));
        }
        return entity.getId();
    }

    /**
     * 分页查询机构类型信息
     * @param entity 查询参数
     * @return IPage<OrganizationType>
     */
    @Cacheable(cacheNames = "organizationtype", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#entity.pageNumber+':'+#entity.pageSize+':'+#entity.orgtype+':'+#entity.orgcode+':'+#entity.orgname")
    @Logable(authName = "租户管理-机构类型管理-分页查询机构类型信息")
    @Override
    public IPage<OrganizationType> List(OrganizationType entity) {
        QueryWrapper<OrganizationType> queryWrapper = new QueryWrapper();
        IPage<OrganizationType> page = new Page();
        if (null != entity.getOrgType()){
            queryWrapper.eq("orgtype", entity.getOrgType());
        }
        if (null != entity.getOrgCode()){
            queryWrapper.eq("orgcode", entity.getOrgCode());
        }
        if(StringUtils.isNotEmpty(entity.getOrgName())) {
            queryWrapper.like("orgname", entity.getOrgName());
        }
        // 默认查询只查询未逻辑删除的数据
        queryWrapper.eq("enabled", 0);
        return baseMapper.selectPage(page,queryWrapper);
    }

    /**
     * 删除机构类型信息(逻辑删除)
     * @param id ID
     * @return boolean
     * @throws ServiceException
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "organizationtype", allEntries = true)
    @Logable(authName = "租户管理-机构类型管理-删除机构类型信息")
    @Override
    public boolean delete(Long id) throws ServiceException {
        OrganizationType entity = this.getById(id);
        int row =0;
        try {
            entity.setEnabled(1);
            row = baseMapper.updateById(entity);
        }catch (ServiceException e) {
            e.getError();
        } catch (Exception e) {
           e.getMessage();
        }

        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除机构类型信息失败"));
        } else {
            return true;
        }
    }

    /**
     * @param
     * @return boolean
     * @description: 新增机构类型
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "organizationType", allEntries = true)
    @Override
    public boolean save(OrganizationType entity) {

        String id = idGeneratorService.queryGeneratorId();

        if (id == null) {
            throw new ServiceException(CommonError.error("新增附件类型失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        entity.setModifiedTime(LocalDateTime.now());
        int row = 0;
        try {
            row = baseMapper.insert(entity);
        }catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增机构类型失败"));
        }
        return true;
    }

    /**
     * @param entity
     * @return boolean
     * @description: 修改机构类型信息
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "organizationType", allEntries = true)
    @Override
    public boolean updateById(OrganizationType entity) {

        int row = 0;
        try {
            entity.setModifiedTime(LocalDateTime.now());
            row = baseMapper.updateById(entity);
        } catch (ServiceException e) {
            e.getError();
        }catch (Exception e) {
            e.getMessage();
        }
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改机构类型失败"));
        }
        return true;
    }

    /**
     * @Author:campchen
     * @Description:
     * @Date:上午 10:45 2019/6/14
     * @Param:
     * @return:
     */
    @Override
    @Cacheable(cacheNames = "Member",key ="#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "机构类型信息-根据机构类型信息进行分页查询")
    public IPage<OrganizationType> getPage(IPage<OrganizationType> page, QueryWrapper<OrganizationType> queryWrapper, String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<OrganizationType> selectAll(@Param("orgType") Integer orgType) {
        return organizationTypeMapper.selectAll(orgType);
    }


}
